package com.ocean.web.controller.index;

import com.github.pagehelper.PageInfo;
import com.ocean.common.core.page.TableDataInfo;
import com.ocean.inxex.domain.SysFtpLog;
import com.ocean.inxex.service.StatisticsSYService;
import com.ocean.inxex.vo.Chenggong;
import com.ocean.inxex.vo.WanChuan;
import com.ocean.inxex.vo.tongJi;
import com.ocean.nc.domain.ONcFile;
import com.ocean.productDict.domain.oNcfileMonitor;
import com.ocean.productRelease.domain.OProductRelease;
import com.ocean.system.domain.TSLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.core.controller.BaseController;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ocean.common.utils.PageUtils.startPage;

/**
 * 统计图表
 */
@Controller
@RequestMapping("/statistics")
public class statisticsSYController {
    @Autowired
    private StatisticsSYService statisticsSYService;

    private String prefix = "system/statistics";

    /**
     * 统计数据
     * @param time
     * @return
     */
    @PostMapping("/lists")
    @ResponseBody
    public List<tongJi> list(String time)
    {
        List<tongJi> tj = statisticsSYService.gettjData(time);
        return tj;
    }
    @PostMapping("/cgdata")
    @ResponseBody
    public Map cgdata(String time)
    {
        List<Chenggong> tj = statisticsSYService.cgdata(time);//成功数量总数
        Integer zs = 0;
        for (int i = 0; i < tj.size(); i++) {
            zs += tj.get(i).getNormalUpload();
        }
        List<ONcFile> list = statisticsSYService.cgdatasize(time);
        Double sum = 0.0;
        for (int i = 0; i < list.size(); i++) {//总量大小
            sum += list.get(i).getFileSize().doubleValue();
        }
        Map map = new HashMap();
        map.put("zs",zs);
        map.put("size",sum);
        return map;
    }

    @PostMapping("/tsdata")
    @ResponseBody
    public Map tsdata(String time)
    {
        List<SysFtpLog> tj = statisticsSYService.tsdata(time);//成功数量总数
        Double sum =0.0;
        for (int i = 0; i < tj.size(); i++) {
            if(tj.get(i).getSize()!=null){
                sum +=tj.get(i).getSize();
            }
        }
        Map map = new HashMap();
        map.put("zs",tj.size());
        map.put("size",bytesToMB(sum));
        return map;
    }
    // 精确计算MB值（保留4位小数）
    public static double bytesToMB(double bytes) {
        if (bytes <= 0) return 0.0;
        return bytes / (1024.0 * 1024.0); // ‌:ml-citation{ref="2,8" data="citationList"}
    }
    @PostMapping("/sbdata")
    @ResponseBody
    public Map sbdata(String time)
    {
        List<TSLog> tj = statisticsSYService.sbdata(time);//

        Map map = new HashMap();
        map.put("zs",tj.size());
        map.put("size",tj);
        return map;
    }
    @PostMapping("/wcdata")
    @ResponseBody
    public Map wcdata(String time)
    {
        List<WanChuan> tj = statisticsSYService.wcdata(time);//晚传数量总数
        List<OProductRelease> wclist = statisticsSYService.wcdatalist(time);
        Integer zs = 0;
        for (int i = 0; i < tj.size(); i++) {
            zs +=tj.get(i).getLateUpload();
        }

        Map map = new HashMap();
        map.put("size",zs);
        map.put("list",wclist);
        return map;
    }

    /**
     * 推送失败量
     * @param time
     * @return
     */
    @PostMapping("/tssbdata")
    @ResponseBody
    public Map tssbdata(String time)
    {

        List<com.ocean.inxex.domain.SysFtpLog> tj = statisticsSYService.tssbdata(time);

        Map map = new HashMap();
        map.put("size",tj.size());
        map.put("list",tj);
        return map;
    }

    /**
     * 推送失败列表
     * @param time
     * @return
     */
    @PostMapping("/tssblist")
    @ResponseBody
    public TableDataInfo tssblist(String time)
    {
        startPage();
        List<com.ocean.inxex.domain.SysFtpLog> tj = statisticsSYService.tssbdata(time);
        return getDataTable(tj);
    }
    @PostMapping("/wcdatasj")
    @ResponseBody
    public TableDataInfo wcdatasj(String time)
    {
        startPage();
        List<OProductRelease> wclist = statisticsSYService.wcdatalist(time);
        return getDataTable(wclist);
    }
    @PostMapping("/sbdatasj")
    @ResponseBody
    public TableDataInfo sbdatasj(String time)
    {
        startPage();
        List<TSLog> tj = statisticsSYService.sbdata(time);//成功数量总数
        return getDataTable(tj);
    }

    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
